scripty2

class Function

Description

Extensions to the built-in Function object.

Methods

Instance methods

  • optionize #

    Function#optionize() -> Function

    Given a function whose last parameter is an object of options, return a function that accepts a variable number of arguments and always passes the last argument that is an object to the options parameter. This way, the function can be called with just options, leaving the other parameters undefined.

    var logOptions = function(a, b, options){
      console.log(options);
    }.optionize();
    logOptions({hello: "world"}) -> logs {hello: "world"}
    logOptions(1, {hello: "world"}) -> logs {hello: "world"}
    logOptions(1, 2, {hello: "world"}) -> logs {hello: "world"}
    

    But note that providing too many arguments will not work:

    logOptions(1,2,3, {hello: "world"}) -> logs 3